package com.lay.controller;

import com.lay.entity.Cars;
import com.lay.entity.InsurContract;
import com.lay.entity.Users;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.lay.service.*;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/lipei")
public class LiPeiguanliController {

    @Autowired
    private CarsService carsService;
    @Autowired
    private InsurContractService contractService;
    @Autowired
    private UsersService usersService;
    /**
     * @param
     * @return
     * @title
     * @description 跳转到报案单导入导出页面
     * @author admin
     * @updateTime 2022/6/10 15:35
     */
    @RequestMapping("/tolipeidan-info")
    public ModelAndView lipeiInfo() {
        return new ModelAndView("lipeidan-info");
    }

    /**
     * @param
     * @return
     * @title
     * @description 下载理赔申请书模板
     * @author admin
     * @updateTime 2022/6/10 16:01
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(@RequestHeader("User-Agent") String agent) throws Exception {
        File file = new File("D:\\Maven\\xyd\\AnKangBaoXian\\src\\main\\resources\\static\\uploadsFile\\理赔申请书.docx");
        //设置响应的说明和额外的参数
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //由注解@RequestHeader("User-Agent")获取到浏览器的类型来解决中文乱码的问题
        String filename = "";
        if (agent.toUpperCase().contains("CHROME")) {
            filename = new String(file.getName().getBytes("utf-8"), "iso-8859-1");
        } else if (agent.toUpperCase().contains("MSIE")) {
            filename = new String(file.getName().getBytes("iso-8859-1"), "utf-8");
        }

        headers.setContentDispositionFormData("attachment", filename);

        //将文件转成byte数组
        byte[] by = FileUtils.readFileToByteArray(file);
        //三个参数分别是一个文件的byte数组，头部信息以及响应码（201）
        ResponseEntity<byte[]> rn = new ResponseEntity<byte[]>(by, headers, HttpStatus.CREATED);
        return rn;
    }
    /**
     * @title
     * @description 上传理赔申请书
     * @author admin
     * @param
     * @updateTime 2022/6/10 17:16
     * @return

     */
    @RequestMapping("/upfile")
    public ModelAndView upFile(@RequestParam(name = "file")MultipartFile file){
        ModelAndView mv = new ModelAndView();
        //得到原来的文件名
        String fileName = file.getOriginalFilename();
        file.getContentType();
        //目标地址
        File dest = new File("D:\\Maven\\xyd\\AnKangBaoXian\\src\\main\\resources\\static\\uploadsFile\\"+fileName);
        try {
            file.transferTo(dest);
            mv.addObject("succ",true);
            mv.addObject("msg","上传成功");
            mv.setViewName("lipeidan-info");
        } catch (IOException e) {
            e.printStackTrace();
            mv.addObject("succ",true);
            mv.addObject("msg","上传失败");
        }
        return mv;
    }
    @RequestMapping("/tolipeidan-add")
    public ModelAndView lipeidanadd(){
        return new ModelAndView("lipeidan-add");
    }
    @RequestMapping("/queryCarById")
    @ResponseBody
    public Map<String,Object> queryCarById(String cId){
        Map<String,Object> map = new HashMap<>();
        Cars cars = carsService.getById(cId);
        boolean flag ;
        if(null==cars){
            flag = false;
        }else {
            Users users = usersService.getById(cars.getuId());
            List<InsurContract> icon = contractService.queryInsurContractById(cId);
            map.put("icon",icon);
            map.put("users",users);
            flag = true;
        }
         map.put("flag",flag);
        return map;
    }
}
